using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Listar : System.Web.UI.Page
{
    Reportes rep = new Reportes();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTotalLibros.Text = rep.CantLibros();
        lblTotalUsuarios.Text = rep.CantUsuarios();

        if (!IsPostBack)
        {
            FillBooksInGrid();
            
        }

    }

    private void FillBooksInGrid()
    {
        DataTable dtBooks = rep.Fetch(User.Identity.Name);

        if (dtBooks.Rows.Count > 0)
        {
            GridView1.DataSource = dtBooks;
            GridView1.DataBind();
        }
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataTable dtBooks = rep.Fetch(User.Identity.Name);
        GridView1.DataSource = dtBooks;
        GridView1.DataBind();

        GridView1.DataSource = SortTable(GridView1.DataSource as DataTable);

        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    private String GridViewSortExpression
    {
        get { return ViewState["SortExp"] as String ?? ""; }
        set { ViewState["SortExp"] = value; }
    }

    public SortDirection GridViewSortDirection
    {

        get
        { if (ViewState["sortDirection"] == null)
          ViewState["sortDirection"] = SortDirection.Ascending;
          return (SortDirection)ViewState["sortDirection"];
        }

        set { ViewState["sortDirection"] = value; }

    }

    

    protected DataView SortTable(DataTable dt)
    {
        DataView dv = new DataView();
        if (dt != null)
        {
            dv = new DataView(dt);
            if (GridViewSortExpression != "")
            {
                dv.Sort = GridViewSortExpression + " " +
                ConvertSortDirectionToSql(GridViewSortDirection);
            }
        }
        return dv;
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dtBooks = rep.Fetch(User.Identity.Name);
        GridView1.DataSource = dtBooks;
        GridView1.DataBind();

        GridViewSortExpression = e.SortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
        }

        DataTable dataTable = GridView1.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(GridViewSortDirection);
            

            GridView1.DataSource = dataView;
            GridView1.DataBind();
        }
    }

}
